﻿using ILRuntime.CLR.Method;
using ILRuntime.CLR.TypeSystem;

namespace Smart.Model
{
	public class ILStaticMethod : IStaticMethod
	{
		private readonly ILRuntime.Runtime.Enviorment.AppDomain appDomain;
		private readonly IMethod method;

		public ILStaticMethod(ILRuntime.Runtime.Enviorment.AppDomain appDomain, string typeName, string methodName, int paramsCount)
		{
			this.appDomain = appDomain;
			IType type = appDomain.LoadedTypes[typeName];
			this.method = type.GetMethod(methodName, paramsCount);
		}

		public override void Run()
		{
			using (var ctx = this.appDomain.BeginInvoke(method))
			{
				ctx.Invoke();
			}
		}

		public override void Run(object a)
		{
			using (var ctx = this.appDomain.BeginInvoke(method))
			{
				ctx.PushObject(a);
				ctx.Invoke();
			}
		}

		public override void Run(object a, object b)
		{
			using (var ctx = this.appDomain.BeginInvoke(method))
			{
				ctx.PushObject(a);
				ctx.PushObject(b);
				ctx.Invoke();
			}
		}

		public override void Run(object a, object b, object c)
		{
			using (var ctx = this.appDomain.BeginInvoke(method))
			{
				ctx.PushObject(a);
				ctx.PushObject(b);
				ctx.PushObject(c);
				ctx.Invoke();
			}
		}
	}
}
